﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace DARE
{
    /// <summary>
    /// 
    /// </summary>
    public class CLNode : ALoadHelper
    {
        #region Fields

        protected CNode m_node;

        private string m_sceneName;
        private string m_renderName;
        private CRender3D m_render;

        #endregion

        #region Property


        public string SceneName
        {
            get
            {
                if (m_render != null && m_render.Scene != null)
                return m_render.Scene.Name;
                return null;
            }
            set
            {
                    ReLoad(null);
            }
        }
        public string RenderName
        {
            get { return this.m_renderName; }
            set
            {
                if (value != this.m_renderName)
                {
                    this.m_renderName = value;
                    ReLoad(null);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ContentSerializerIgnore]
        public CNode Data
        {
            get { return this.m_node; }
            protected set
            {
                if (this.m_node != value)
                    this.m_node = value;
            }
        }

        #endregion

        #region CTOR

        /// <summary>
        /// 
        /// </summary>
        public CLNode()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        public CLNode(string _path)
            : base(_path)
        {
        }

        public CLNode(CNode node)
            : base(node.Name)
        {
            this.m_node = node;
            RenderName = node.Node.Render3D.Name;
            m_render = node.Render3D;
        }

        #endregion

        #region Method

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lastPath"></param>
        protected override void ReLoad(string lastPath)
        {
            if (CDare.Instance == null)
                return;
            if (this.Data != null || string.IsNullOrEmpty(this.SceneName) || string.IsNullOrEmpty(this.RenderName) || string.IsNullOrEmpty(base.Path))
                return;
            this.Data = CDare.Instance.SceneMgr.GetScene(SceneName).Render3D[RenderName].GetNode(base.Path);
            //if (!string.IsNullOrEmpty(lastPath))
            //    CResourceMgr.UnLoad(lastPath);
            base.ReLoad(lastPath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public static implicit operator CNode(CLNode _node)
        {
            return _node.Data;
        }
        #endregion
    }
}
